<?php

namespace App\Ship\Parents\Requests;

use Apiato\Core\Abstracts\Requests\Request as AbstractRequest;
use App\Containers\AppSection\Profile\Models\Profile;
use App\Containers\AppSection\Profile\Tasks\FindProfileByIdTask;
use App\Ship\Exceptions\AuthenticationException;
use App\Ship\Exceptions\NotFoundException;
use Exception;
use Lcobucci\JWT\Configuration;
use Apiato\Core\Abstracts\Models\UserModel as User;

/**
 * @property int $id
 */
abstract class Request extends AbstractRequest
{
    public function profile(): Profile
    {
        try {
            $profileId = Configuration::forUnsecuredSigner()
                ->parser()
                ->parse($this->bearerToken())
                ->claims()
                ->get('profileId');

            if (empty($profileId)) {
                throw new NotFoundException(__('ship::resource.not-found'));
            }

            return app(FindProfileByIdTask::class)->run($profileId);
        } catch (Exception $e) {
            throw new AuthenticationException(__('ship::auth.profile'));
        }

    }
}
